package com.xinai.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.xinai.util.DBUtil;
import com.xinai.util.Logger;
import com.xinai.vo.Department;
import com.xinai.vo.SysBarcode;

import java.util.List;


public class sysBarcodeDao extends BaseDao {

    private static final String TABLE = "sysBarcode";
    public sysBarcodeDao(Context context){
        super(context);
    }
 //查询一条记录
    public SysBarcode findFirst() {
        return callBack(TYPE_READ, new DaoCallBack<SysBarcode>() {
            @Override
            public SysBarcode invoke(SQLiteDatabase conn) {
                cursor = conn.query(TABLE, null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    SysBarcode sysBarcode = new SysBarcode();
                    sysBarcode.setProductID(cursor.getString(cursor.getColumnIndex("productID")));
                    sysBarcode.setProductName(cursor.getString(cursor.getColumnIndex("productName")));
                    sysBarcode.setId(cursor.getInt(cursor.getColumnIndex("id")));
                    sysBarcode.setColorID(cursor.getString(cursor.getColumnIndex("colorID")));
                    sysBarcode.setColorNo(cursor.getString(cursor.getColumnIndex("colorNo")));
                    sysBarcode.setColorName(cursor.getString(cursor.getColumnIndex("colorName")));
                    sysBarcode.setSizesID(cursor.getString(cursor.getColumnIndex("sizesID")));
                    sysBarcode.setSizesNo(cursor.getString(cursor.getColumnIndex("sizesNo")));
                    sysBarcode.setSizesName(cursor.getString(cursor.getColumnIndex("sizesName")));
                    sysBarcode.setBarcode(cursor.getString(cursor.getColumnIndex("barcode")));
                    return sysBarcode;
                }
                return null;
            }
        });
    }
    public List<SysBarcode> getList(final String parma) {
        return callBack(TYPE_READ, new DaoCallBack<List<SysBarcode>>() {
            @Override
            public List<SysBarcode> invoke(SQLiteDatabase conn) {
                String sql = " select * from sysBarcode where productName like ? or productNo like ? ";
                cursor = conn.rawQuery(sql, new String[] {"%" + parma + "%", "%" + parma + "%"});
                if (cursor == null) {
                    return null;
                }
                List<SysBarcode> departments = DBUtil.cursor2VOList(cursor, SysBarcode.class);
                return departments;
            }
        });
    }
    //根据条码查询
    public List<SysBarcode> getListbybarcode(final String parma){
        return callBack(TYPE_READ, new DaoCallBack<List<SysBarcode>>() {
            @Override
            public List<SysBarcode> invoke(SQLiteDatabase conn) {
                String sql = " select barcode,productID,productNo,productName,colorID,colorNo,colorName,sizesID,sizesName,sizesNo from sysBarcode where barcode= ? ";
                cursor = conn.rawQuery(sql, new String[] {parma});
                if (cursor == null) {
                    return null;
                }
                List<SysBarcode> departments = DBUtil.cursor2VOList(cursor, SysBarcode.class);
                return departments;
            }
        });

    }

public int total(){
    return callBack(TYPE_READ, new DaoCallBack<Integer>() {

        @Override
        public Integer invoke(SQLiteDatabase conn) {
            cursor =  conn.rawQuery("select count(*)  from sysBarcode ",null);
            int total=0;
            while (cursor.moveToNext()) {
                 total = cursor.getInt(0);
                System.out.print("总数：aaa" + total);
            }
          //  int t=Integer.parseInt(total);
           return total;
        }
    });

}


 //写入与修改记录
 public int insert(final SysBarcode sysBarcode) {
     return callBack(TYPE_WRITE, new DaoCallBack<Integer>() {
         @Override
         public Integer invoke(SQLiteDatabase conn) {
             conn.execSQL("replace into sysBarcode(productID,productNo,productName,colorID,colorNo,colorName, sizesID,sizesNo,sizesName,barcode) values(?,?,?,?,?,?,?,?,?,?)",
                     new Object[] {sysBarcode.getProductID(), sysBarcode.getProductNo(), sysBarcode.getProductName(),sysBarcode.getColorID(),sysBarcode.getColorNo(),sysBarcode.getColorName(), sysBarcode.getSizesID(),sysBarcode.getSizesNo(),sysBarcode.getSizesName(),sysBarcode.getBarcode()});
             return 1;
         }
     });
 }


}
